package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Singer;
import com.softeem.music_sys.service.SingerService;
import com.softeem.music_sys.mapper.SingerMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author RD
* @description 针对表【singer(歌手信息表)】的数据库操作Service实现
* @createDate 2025-11-07 11:19:17
*/
@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer>
    implements SingerService{

    @Override
    public Result addSinger(Singer singer) {
        // 检查歌手名是否已存在
        QueryWrapper<Singer> wrapper = new QueryWrapper<>();
        wrapper.eq("name", singer.getName());
        Singer existSinger = this.getOne(wrapper);
        if (existSinger != null) {
            return Result.fail("歌手已存在");
        }
        
        // 设置默认状态
        singer.setStatus(1);
        
        boolean result = this.save(singer);
        return result ? Result.success().setMessage("添加成功") : Result.fail("添加失败");
    }

    @Override
    public Result getSingerById(Integer id) {
        Singer singer = this.getById(id);
        if (singer == null) {
            return Result.fail("歌手不存在");
        }
        return Result.success().setData(singer);
    }

    @Override
    public Result updateSinger(Singer singer) {
        Singer existSinger = this.getById(singer.getId());
        if (existSinger == null) {
            return Result.fail("歌手不存在");
        }
        boolean result = this.updateById(singer);
        return result ? Result.success().setMessage("更新成功") : Result.fail("更新失败");
    }

    @Override
    public Result deleteSinger(Integer id) {
        Singer singer = this.getById(id);
        if (singer == null) {
            return Result.fail("歌手不存在");
        }
        boolean result = this.removeById(id);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }

    @Override
    public Page<Singer> getSingerList(Integer page, Integer limit) {
        Page<Singer> pageUtil = new Page<>(page, limit);
        return page(pageUtil);
    }

    @Override
    public Page<Singer> getSingersByType(String type, Integer page, Integer limit) {
        Page<Singer> pageUtil = new Page<>(page, limit);
        QueryWrapper<Singer> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("status", 1);
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<Singer> getSingersByTag(String tag, Integer page, Integer limit) {
        Page<Singer> pageUtil = new Page<>(page, limit);
        QueryWrapper<Singer> wrapper = new QueryWrapper<>();
        wrapper.like("tags", tag);
        wrapper.eq("status", 1);
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<Singer> searchSingers(String keyword, Integer page, Integer limit) {
        Page<Singer> pageUtil = new Page<>(page, limit);
        QueryWrapper<Singer> wrapper = new QueryWrapper<>();
        wrapper.like("name", keyword);
        wrapper.eq("status", 1);
        return page(pageUtil, wrapper);
    }

    @Override
    public Result updateSingerStatus(Integer id, Integer status) {
        Singer singer = this.getById(id);
        if (singer == null) {
            return Result.fail("歌手不存在");
        }
        singer.setStatus(status);
        boolean result = this.updateById(singer);
        return result ? Result.success().setMessage("状态更新成功") : Result.fail("状态更新失败");
    }
}